package com.taxi.driver.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taxi.driver.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 账单里的日期左右切换栏
 */
public class SwitchTable extends RelativeLayout implements View.OnClickListener{
    private RelativeLayout rl_left,rl_right;
    private TextView tv_text1,tv_text2;
    private String curMonth,startDay,endDay;
    private DateListener listener;

    public final int WEEK = 0;
    public final int MONTH = 1;
    private int MODE = MONTH;

    private String PATTERM_MONTH = "yyyy-MM-dd";
    private String PATTERM_DAY = "yyyy.MM.dd";

    public SwitchTable(Context context) {
        this(context, null);
    }

    public SwitchTable(Context context, AttributeSet attrs) {
        this(context, attrs ,0);
    }

    public SwitchTable(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_switch_table,null);
        rl_left = (RelativeLayout) view.findViewById(R.id.rl_left);
        rl_right = (RelativeLayout) view.findViewById(R.id.rl_right);
        tv_text1 = (TextView) view.findViewById(R.id.tv_text1);
        tv_text2 = (TextView) view.findViewById(R.id.tv_text2);

        rl_left.setOnClickListener(this);
        rl_right.setOnClickListener(this);
        addView(view);

        curMonth = date2Str(new Date(),PATTERM_MONTH);
        tv_text1.setText("0.0");
        tv_text2.setText(curMonth.substring(0,7));

        startDay = getMonday(str2Date(curMonth,PATTERM_MONTH));
        endDay = getAddDay(startDay,6);
    }

    public TextView getTitleView(){
        return tv_text1;
    }

    /**
     * 模式切换
     * @param mode
     */
    public void switchMode(int mode){
        if(mode == MONTH){
            if(MODE == MONTH){
                tv_text2.setText(curMonth.substring(0,7));
            }else{
                curMonth = date2Str(str2Date(startDay,PATTERM_DAY),PATTERM_MONTH);
                tv_text2.setText(curMonth.substring(0,7));
            }
            if(listener != null)
                listener.onMonth(tv_text1,curMonth.substring(0,7));
        }else{
            if(MODE == MONTH){
                startDay = getMonday(str2Date(curMonth,PATTERM_MONTH));
                endDay = getAddDay(startDay,6);
                tv_text2.setText("("+startDay+" - "+endDay+")");
            }else{
                tv_text2.setText("("+startDay+" - "+endDay+")");
            }
            if(listener != null)
                listener.onWeek(tv_text1,paseDate(startDay),paseDate(endDay));
        }

        MODE = mode;
    }

    /**
     * 获得指定日期所在周的星期一的日期
     * @param date
     */
    private String getMonday(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        date2Str(date,PATTERM_DAY);
        int add = 0;
        switch (c.get(Calendar.DAY_OF_WEEK)) {
            case 1:
                add = -6;
                break;
            case 2:
                add = 0;
                break;
            case 3:
                add = -1;
                break;
            case 4:
                add = -2;
                break;
            case 5:
                add = -3;
                break;
            case 6:
                add = -4;
                break;
            case 7:
                add = -5;
                break;
        }
        c.add(Calendar.DAY_OF_MONTH,add);
        return date2Str(c.getTime(),PATTERM_DAY);
    }

    /**
     * 获得"月"模式下，当前的月份
     * @return
     */
    public String getMonth(){
        return curMonth.substring(0,7);
    }

    /**
     * 获得指定月分的最大天数
     * @param month 指定的月份（格式2017-01）
     * @return
     */
    public String getDaysForMoth(String month){
        SimpleDateFormat dateFormater = new SimpleDateFormat(
                "yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(str2Date(month,PATTERM_MONTH));
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH,
                cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dateFormater.format(cal.getTime());
    }

    /**
     * 获得"周"模式下，起始日期
     * @return
     */
    public String getStartDay(){
        return startDay;
    }

    /**
     * 获得"周"模式下，结束日期
     * @return
     */
    public String getEndDay(){
        return endDay;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_left:
                sub();
                break;
            case R.id.rl_right:
                add();
                break;
        }
    }

    /**
     * Date转日期字符串
     * @return
     */
    private String date2Str(Date date,String patterm){
        SimpleDateFormat sf=new SimpleDateFormat(patterm);
        return sf.format(date);
    }

    /**
     * 时间字符串转Date
     * @param str
     * @return
     */
    private Date str2Date(String str,String patterm) {
        SimpleDateFormat sf=new SimpleDateFormat(patterm);
        Date date = null;
        try {
            date = sf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获得指定日期增加add月后的月份
     * @param date
     * @return
     */
    private String getAddMonth(Date date,int add){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH,add);
        return date2Str(c.getTime(),PATTERM_MONTH);
    }

    /**
     * 获得指定日期增加add天后的日期
     * @param startDate
     * @param add
     * @return
     */
    private String getAddDay(String startDate,int add){
        Date date = str2Date(startDate,PATTERM_DAY);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR,add);
        return date2Str(c.getTime(),PATTERM_DAY);
    }

    /**
     * 向右切换
     */
    private void add(){
        if(MODE == MONTH){
            curMonth = getAddMonth(str2Date(curMonth,PATTERM_MONTH),1);
            tv_text2.setText(curMonth.substring(0,7));

            if(listener != null)
                listener.onMonth(tv_text1,curMonth.substring(0,7));
        }else{
            startDay = getAddDay(endDay,1);
            endDay = getAddDay(startDay,6);
            tv_text2.setText("("+startDay+" - "+endDay+")");

            if(listener != null)
                listener.onWeek(tv_text1,paseDate(startDay),paseDate(endDay));
        }

    }

    /**
     * 向左切换
     */
    private void sub(){
        if(MODE == MONTH){
            curMonth = getAddMonth(str2Date(curMonth,PATTERM_MONTH),-1);
            tv_text2.setText(curMonth.substring(0,7));

            if(listener != null)
                listener.onMonth(tv_text1,curMonth.substring(0,7));
        }else{
            endDay = getAddDay(startDay,-1);
            startDay = getAddDay(endDay,-6);
            tv_text2.setText("("+startDay+" - "+endDay+")");

            if(listener != null)
                listener.onWeek(tv_text1,paseDate(startDay),paseDate(endDay));
        }
    }

    /**
     * 格式转换：2017.01.01 --> 2017-01-01
     * @param str
     * @return
     * @throws ParseException
     */
    public String paseDate(String str){
        Date date = str2Date(str,PATTERM_DAY);
        return date2Str(date,PATTERM_MONTH);
    }

    public interface DateListener{
        void onMonth(TextView fee,String month);
        void onWeek(TextView fee,String startDay,String endDay);
    }

    public void setDateListener(DateListener listener){
        this.listener=listener;
    }
}